var NoteActions = require('../../actions/NoteActions.jsx');
var NoteStore = require('../../stores/NoteStore.jsx');
var React = require('react');
var TextArea = require('./TextArea.jsx');

var NoteCreationBox = React.createClass({
    getInitialState(){
        return {
            id: this.props.id
        }
    },
    handleSave: function (noteText, id) {
        if (id) {
            NoteActions.editNote({_id: id, text: noteText});
        } else {
            var _id = Date.now();
            NoteActions.createNote({_id: _id, text: noteText});
            this.setState({
                id: _id
            });
        }
    },
    render: function () {
        var _id = this.state.id;
        var note = NoteStore.getNote(_id);
        console.log(_id, note);
        return (
            <div className="col-md-8">
                <TextArea onSave={this.handleSave.bind(this)} id={_id} noteText={note ? note.text : ''}/>
            </div>
        );
    }
});

module.exports = NoteCreationBox;